<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="用户编号" prop="platformUserId">
        <el-input
          v-model="queryParams.platformUserId"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款方式" prop="payChannel">
        <el-select clearable v-model="queryParams.payChannel" placeholder="请选择退款方式">
          <el-option
            v-for="item in plat_order_pay_channel"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="退款审核状态" prop="state">
        <el-select clearable v-model="queryParams.state" placeholder="请选择">
          <el-option
            v-for="item in refund_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="订单编号" prop="outTradeNo">
        <el-input
          v-model="queryParams.outTradeNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核时间">
        <el-date-picker clearable
          v-model="searchDate"
          type="daterange"
          value-format="YYYY-MM-DD"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="confirmSearchDate">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-10px flex items-center">
      <div class="total">
        <span>累计退款金额：</span>
        <span class="amount">{{ formatNumberWithCommas(formatAmount(summary.cash_fee)) }}</span>
        <span>元</span>
      </div>
      <div class="total">
        <span>累计退款订单数：</span>
        <span class="amount">{{ formatNumberWithCommas(total) }}</span>
        <span>笔</span>
      </div>
    </div>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb-10px">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['bus:platUserRefundRecord:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table class="fixed-scrollbar" v-horizontal-scroll border v-loading="loading" :data="refundList">
        <el-table-column label="来源渠道" align="center" prop="channelCode" min-width="150" show-overflow-tooltip />
        <el-table-column label="用户编号" align="center" prop="platformUserId" min-width="150" show-overflow-tooltip />
        <el-table-column label="手机号码" align="center" prop="mobile" width="120" />
        <el-table-column label="电子卡号" align="center" prop="cardNo" width="150" show-overflow-tooltip />
        <el-table-column label="退款方式" align="center" prop="payChannel" width="100">
          <template #default="scope">
            <dict-tag :options="plat_order_pay_channel" :value="scope.row.payChannel" />
          </template>
        </el-table-column>
        <el-table-column label="退款金额（元）" align="center" prop="refundFee" width="120">
          <template #default="scope">
            <span>{{ formatAmount(scope.row.refundFee) }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="退款后余额" align="center" prop="balance" width="110">
          <template #default="scope">
            <span>{{ formatAmount(scope.row.balance) }}</span>
          </template>
        </el-table-column> -->
        <!-- <el-table-column label="退款审核状态" align="center" prop="state" width="110">
          <template #default="scope">
            <dict-tag :options="refund_status" :value="scope.row.state" />
          </template>
        </el-table-column> -->
        <el-table-column label="退款申请时间" align="center" prop="createTime" width="180" />
        <el-table-column label="退款审核时间" align="center" prop="throughTime" width="180" />
        <el-table-column label="订单编号" align="center" prop="outTradeNo" min-width="180" show-overflow-tooltip />
        <el-table-column label="交易编号" align="center" prop="transactionId" min-width="180" show-overflow-tooltip />
      </el-table>
    </div>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Refund">
import { listRefund, getRefund } from "@/api/order/refund"

const { proxy } = getCurrentInstance()
const { refund_status, plat_order_pay_channel } = proxy.useDict('refund_status', 'plat_order_pay_channel')

const refundList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const summary = ref({})
const searchDate = ref([])
const showDialog = ref(false)
const dialogTitle = ref('')

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    platformUserId: null,
    mobile: null,
    outTradeNo: null,
    state: 2,
    payChannel: null,
    beginDate: null,
    endDate: null
  }
})

const { queryParams } = toRefs(data)

const confirmSearchDate = (e) => {
  if (e && e.length) {
    queryParams.value.beginDate = e[0]
    queryParams.value.endDate = e[1]
  } else {
    queryParams.value.beginDate = null
    queryParams.value.endDate = null
  }
  handleQuery()
}

/** 查询退款记录列表 */
function getList() {
  loading.value = true
  let params = {
    ...queryParams.value,
    'params[auditBeginTime]': queryParams.value.beginDate,
    'params[auditEndTime]': queryParams.value.endDate,
    orderByType: 2,                   // 1 审核列表 2退款成功列表，用来排序
  }
  delete params.beginDate
  delete params.endDate
  listRefund(params).then(response => {
    refundList.value = response.rows
    total.value = response.total
    loading.value = false
    summary.value = response.remark || {}
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  searchDate.value = []
  queryParams.value.beginDate = null
  queryParams.value.endDate = null
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('bus/platUserRefundRecord/export', {
    ...queryParams.value
  }, `退款订单_${proxy.getFileImportName()}.xlsx`)
}

getList()
</script>

<style lang="scss" scoped>
.total {
  width: 50%;
  color: #606266;
  font-size: 14px;
  padding: 8px 0;

  .amount {
    color: #333;
    font-size: 18px;
    padding-right: 3px;
    font-weight: bold;
  }
}
</style>

